package com.cadre.system.service.impl;

import com.cadre.common.exception.CadreException;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.common.utils.oldutil.ResultEnum;
import com.cadre.common.utils.oldutil.ResultVO;
import com.cadre.common.utils.oldutil.ResultVOUtil;
import com.cadre.system.domain.officer.PerSonRenmVO;
import com.cadre.system.mapper.ReportRenmMapper;
import com.cadre.system.service.ReportRenmService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReportRenmServiceImpl implements ReportRenmService {
    @Autowired
    ReportRenmMapper reportRenmMapper;

    @Override
    public ResultVO selectPerSonRenmListById(String ids) {
        if (StringUtils.isEmpty(ids)) {
            throw new CadreException(ResultEnum.NULL_ERROR);
        }
        String[] str = ids.split(",");
        List<PerSonRenmVO> list = new ArrayList<>();
        for (String str2 : str) {
            list.addAll(this.reportRenmMapper.selectPerSonRenmListById(str2));
        }
        for (PerSonRenmVO vo : list) {
            getName(vo);
            getAa01(vo);
            getAa02(vo);
            getAa03(vo);
        }
        return ResultVOUtil.success(list);
    }

    private void getName(PerSonRenmVO vo) {
        String a0105 = vo.getA0105();
        boolean b = StringUtils.isNotEmpty(a0105) && "女".equals(a0105);
        String a0130 = vo.getA0130();
        boolean b1 = StringUtils.isNotEmpty(a0130) && !"汉族".equals(a0130);
        StringBuilder sb = new StringBuilder();
        if (b && !b1) {
            sb.append("(女)");
        }
        if (b && b1) {
            sb.append("(女,");
            sb.append(a0130);
            sb.append(")");
        }
        if (!b && b1) {
            sb.append("(");
            sb.append(a0130);
            sb.append(")");
        }
        if (StringUtils.isNotBlank(sb.toString())) {
            vo.setA0101(vo.getA0101() + "\n" + sb.toString());
        }
    }

    private void getAa01(PerSonRenmVO vo) {
        Date a0106 = vo.getA0106();
        if (a0106 != null) {
            String time = new SimpleDateFormat(DateUtil.MONTG_DATE_FORMAT).format(a0106);
            vo.setAa01(time);
        }
    }

    private void getAa02(PerSonRenmVO vo) {
        String a0179 = vo.getA0179();
        Date a0180 = vo.getA0180();
        String time = null;
        if (a0180 != null) {
            time = new SimpleDateFormat(DateUtil.MONTG_DATE_FORMAT).format(a0180);
        }
        if (!StringUtils.isNotBlank(a0179)) {
            return;
        }
        if (time != null) {
            vo.setAa02(time + "\n" + a0179);
            return;
        }
        vo.setAa02(a0179);
    }

    private void getAa03(PerSonRenmVO vo) {
        String a2205 = vo.getA2205();
        Date a2210 = vo.getA2210();
        String time = null;
        if (a2210 != null) {
            time = new SimpleDateFormat(DateUtil.MONTG_DATE_FORMAT).format(a2210);
        }
        if (!StringUtils.isNotBlank(time)) {
            return;
        }
        if (!StringUtils.isNotBlank(a2205) || "中共党员".equals(a2205)) {
            vo.setAa03(time);
            return;
        }
        vo.setAa03(time + "\n(" + a2205 + ")");
    }
}
